import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLaunchPage extends StatefulWidget {
  const UrlLaunchPage({super.key});

  @override
  State<UrlLaunchPage> createState() => _UrlLaunchPageState();
}

class _UrlLaunchPageState extends State<UrlLaunchPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Flutter App"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: () async {
              const url ="https://www.baidu.com";
              if(!await launchUrl(Uri.parse(url))){
                throw Exception("error");
              }
            }, child: const Text("打开外部浏览器")),
            ElevatedButton(onPressed: () async {
              const url ="tel:10086";
              if(!await launchUrl(Uri.parse(url))){
              throw Exception("error");
              }
            }, child: const Text("拨打电话")),
            ElevatedButton(onPressed: () async {
              const url ="sms:10086";
              if(!await launchUrl(Uri.parse(url))){
              throw Exception("error");
              }
            }, child: const Text("发送短信")),
            ElevatedButton(onPressed: () async {
              const url ="alipays://";
              if(!await launchUrl(Uri.parse(url))){
              throw Exception("error");
              }
            }, child: const Text("打开外部应用")),
          ],
        ),
      ),
    );
  }
}
